package com.zbkj.front.controller.user;

import com.zbkj.common.exception.CrmebException;
import com.zbkj.service.service.user.request.UpdateAccountPwdRequest;
import com.zbkj.common.request.UserEditRequest;
import com.zbkj.common.request.WxBindingPhoneRequest;
import com.zbkj.common.response.CommonResult;
import com.zbkj.common.response.UserCenterResponse;
import com.zbkj.common.vo.FileResultVo;
import com.zbkj.service.service.user.UserService;
import com.zbkj.service.thirdparty.files.UploadService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

@Slf4j
@RestController()
@RequestMapping("api/front/user/info")
@Api(tags = "用户-用户信息")
public class UserInfoController {

    @Autowired
    private UserService userService;
    @Autowired
    private UploadService uploadService;

    @ApiOperation(value = "用户-绑定手机号-(普通)")
    @RequestMapping(value = "/bindPhone", method = RequestMethod.POST)
    @ApiImplicitParams({
            @ApiImplicitParam(name = "phone",value = "手机号"),
            @ApiImplicitParam(name = "captcha",value = "验证码")
    })
    public CommonResult<Boolean> bindPhone(String phone, String captcha){
        return CommonResult.success(userService.bindPhone(phone,captcha));
    }

    @ApiOperation(value = "用户-绑定手机号-(获取微信用户绑定的手机号)")
    @RequestMapping(value = "/bindPhoneWeixin", method = RequestMethod.POST)
    public CommonResult<Boolean> bindPhoneWeixin(@RequestBody @Validated WxBindingPhoneRequest request){
        return CommonResult.success(userService.bindPhoneWeixin(request));
    }

    @ApiOperation(value = "用户-获取用户个人信息")
    @RequestMapping(value = "/getUserInfo", method = RequestMethod.GET)
    public CommonResult<UserCenterResponse> getUserInfo() {
        return CommonResult.success(userService.getUserCenter());
    }

    @ApiOperation(value = "用户-修改个人资料")
    @RequestMapping(value = "/editUserInfo", method = RequestMethod.POST)
    public CommonResult<Object> editUserInfo(@RequestBody @Validated UserEditRequest request) {
        if (userService.editUser(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "用户-修改昵称")
    @RequestMapping(value = "/userNickNameUpdate", method = RequestMethod.GET)
    public CommonResult<Object> userNickNameUpdate(String nickName) {
        UserEditRequest request = new UserEditRequest();
        request.setNickname(nickName);
        if (userService.editUser(request)) {
            return CommonResult.success();
        }
        return CommonResult.failed();
    }

    @ApiOperation(value = "用户-修改账号密码")
    @RequestMapping(value = "/updateAccountPwd", method = RequestMethod.POST)
    public CommonResult<Boolean> updateAccountPwd(@RequestBody @Validated UpdateAccountPwdRequest request) {
        return CommonResult.success(userService.updateAccountPwd(request));
    }

    @ApiOperation(value = "用户-更换头像")
    @RequestMapping(value = "/userUpdatePhoto", method = RequestMethod.POST)
    public CommonResult<FileResultVo> userUpdatePhoto(@RequestParam(value = "multipart") MultipartFile multipart) {
        try{
            FileResultVo fileResultVo = uploadService.imageUploadNew(multipart,"user/head",11);
            if (fileResultVo != null){
                UserEditRequest request = new UserEditRequest();
                request.setAvatar(fileResultVo.getUrl());
                userService.editUser(request);
            }

            return CommonResult.success(fileResultVo);
        }catch (Exception e){
            e.printStackTrace();
            throw new CrmebException(e.getMessage());
        }
    }

}
